<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\UserInfo;

/**
 * UserInfoSearch represents the model behind the search form about `common\models\UserInfo`.
 */
class UserInfoSearch extends UserInfo
{
    /**
     * @inheritdoc
     */
	public $created_at_start;
	public $created_at_end;
	public $status_1;
	public $user_type_1;
	public $user_from_1;
	public $pageSize;
    public function rules()
    {
        return [
            [['id', 'confirmed_at', 'created_at', 'lastlogin_at', 'updated_at', 'blocked_at'], 'integer'],
            [['username', 'nickname', 'tel', 'email', 'password_hash', 'auth_key', 'registration_ip', 'lastlogin_ip', 'status', 'user_type', 'user_from'], 'safe'],
            [['created_at_start','created_at_end','status_1','user_type_1','user_from_1','pageSize'],'safe'],
            ['status_1','in','range'=>['正常','删除','锁定'],'message'=>'请填入正常|删除|锁定'],
            ['user_type_1','in','range'=>['普通用户','VIP会员'],'message'=>'请填入普通用户|VIP会员'],
            ['user_from_1','in','range'=>['手机','QQ','微博','微信','邮件','小程序'],'message'=>'请填入手机|QQ|微博|微信|邮件|小程序']
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params,$pageSize=0)
    {
    	//初次加载，不进行查询
    	if(!array_key_exists("UserInfoSearch", $params)){
    		$query = UserInfo::find()->where("1>2"); 

    		$dataProvider = new ActiveDataProvider([
    				'query' => $query,
    				'Pagination'=> ['pageSize' => $pageSize],
    		]);
    		return $dataProvider;
    	}
        $query = UserInfo::find();
        $this->load($params);
		if($this->pageSize == 1){
			$pageSize = '5';
		}
		
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        	'Pagination'=> ['pageSize' => $pageSize],
        ]);

        $dataProvider->setSort([
        	'attributes' => [
	        'id',
	        'username',
	        'nickname',
	        'tel',
	        'email',
	        'password_hash',
	        'auth_key',
	        
	        'registration_ip' ,
	        'created_at',
	        'lastlogin_ip',
	        'lastlogin_at',
	        'updated_at',
	        'status',
	        'user_type',
	        'user_from',
	        'blocked_at',
	        		 
	        	'created_at_start'=>[
	        		'asc' => ['created_at' =>SORT_ASC],
	        		'desc' => ['created_at' =>SORT_DESC],
	        		'label'=> Yii::t('app','Created At')
	        		],
        		'status_1'=>[
        			'asc' => ['status' =>SORT_ASC],
        			'desc' => ['status' =>SORT_DESC],
        			'label'=> Yii::t('app','Status')
        				],
        		'user_type_1'=>[
        			'asc' => ['user_type' =>SORT_ASC],
        			'desc' => ['user_type' =>SORT_DESC],
        			'label'=> Yii::t('app','User Type')
        				],
        		'user_from_1'=>[
        			'asc' => ['user_from' =>SORT_ASC],
        			'desc' => ['user_from' =>SORT_DESC],
        			'label'=> Yii::t('app','User From')
        				],      		 
        		],
        		]);
        


        

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'confirmed_at' => $this->confirmed_at,
            'created_at' => $this->created_at,
            'lastlogin_at' => $this->lastlogin_at,
            'updated_at' => $this->updated_at,
            'blocked_at' => $this->blocked_at,
        ]);

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'nickname', $this->nickname])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'registration_ip', $this->registration_ip])
            ->andFilterWhere(['like', 'lastlogin_ip', $this->lastlogin_ip])
            ->andFilterWhere(['like', 'status', $this->status])
            ->andFilterWhere(['in', 'user_type', $this->user_type])
            ->andFilterWhere(['in', 'user_from', $this->user_from])
            //->andFilterWhere(['like', 'FROM_UNIXTIME(created_at, "%Y-%m-%d %H:%i:%S")', $this->created_at_start])
        	->andFilterWhere(['between', 'FROM_UNIXTIME(created_at, "%Y-%m-%d")', $this->created_at_start,$this->created_at_end])
            ->andFilterWhere(['like', 'status', $this->setStatus($this->status_1)])
            ->andFilterWhere(['in', 'user_type', $this->setType($this->user_type_1)])
            ->andFilterWhere(['in', 'user_from', $this->setFrom($this->user_from_1)])
        ;
		//不显示搜索结果
		
        return $dataProvider;
    }
    
    public function setStatus($string)
    {    	
    	$_arr = [
    	''=>'',
    	'正常'=>'1',
    	'删除'=>'2',
    	'锁定'=>'3'
    			];
    	return $_arr[$string];
    }
    public function setType($string)
    {
        $_arr = [
	        ''=>'',
			'游客'=>'0',
			'注册用户(一般用户)'=>'1',
			'VIP会员(申请中)'=>'2',
			'认证VIP会员(审核通过)'=>'3',
			'装饰公司'=>'4',
			'城市运营商'=>'5',
			'协会'=>'6',
        ];
    	return $_arr[$string];
    }
    public function setFrom($string)
    {
        $_arr = [
	         ''=>'',
	        '手机'=>'1',
	        'QQ'=>'2',
	        '微博'=>'3',
	        '微信'=>'4',
	        '邮件'=>'5',
             '小程序'=>'6'
        ];
    	return $_arr[$string];
    }
    
}
